在 C++ 的劇場中,每個物件都有其生命週期—— 物件生命週期。這定義了物件在記憶體中佔用空間的時間長度。在 函式主體中,預設行為是 局部變數 為 自動,但我們可以命令它們成為 靜態 以改變它們的命運。
1. 自動物件
預設情況下,局部變數是 自動物件。它們在 函式 執行到其定義時誕生(初始化),並在程式碼區塊結束時死亡(被回收)。它們位於堆疊上,因此每次呼叫都會產生全新的實例。
2. 局部靜態物件
當您使用 靜態 關鍵字時,就會建立一個 局部靜態物件。這些物件僅初始化一次——在第一次控制流經其定義之前——並一直存活至程式的終止。這使得函式能「記住」狀態,而不會污染全域範圍。
3. 遞迴陷阱
在一個 遞迴函式中,每一次 遞迴循環 都會創建其自動物件的一個獨立實例。如果遞迴層次很深,這將消耗大量堆疊空間。相反地,一個 靜態 物件會在該遞迴的所有層級之間共享。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Where do automatic objects reside in memory?
The Heap
The Stack
The Static Data Segment
The Register
✅ Correct!
Automatic objects are local to the function execution and are stored on the stack.❌ Incorrect
The stack handles temporary local storage; the data segment is for globals and statics.QUESTION 2
How many times is a local static object initialized?
Every time the function is called.
Never; they are always zero-initialized.
Exactly once, before the first call execution reaches its definition.
Twice: once at compile-time and once at runtime.
✅ Correct!
Statics persist. The compiler ensures they are only initialized once throughout the program's life.❌ Incorrect
If it initialized every time, it wouldn't be 'static'—it would behave like an automatic object.QUESTION 3
What happens to a local static variable when the function returns?
It is destroyed and memory is freed.
It remains in memory and retains its value for the next call.
It becomes a global variable accessible by any function.
Its value is reset to the default constructor.
✅ Correct!
The object lifetime persists until the program ends, though its 'scope' remains local to the function.❌ Incorrect
While its lifetime is global, its visibility (scope) is still restricted to the function block.QUESTION 4
In a recursive function, what is the behavior of an automatic object?
A single instance is shared among all recursive calls.
A new instance is created for each call on the stack.
It is moved to the heap to prevent stack overflow.
It is converted to a static object by the compiler.
✅ Correct!
Each recursive call has its own stack frame, and thus its own local automatic variables.❌ Incorrect
Automatic objects are uniquely instantiated per function activation.QUESTION 5
Which keyword prevents 'global scope pollution' while still keeping variable state across calls?
extern
const
static
volatile
✅ Correct!
Local static objects provide persistence while hiding the variable inside the function scope.❌ Incorrect
The 'static' keyword within a function scope achieves local persistence.Exercise 7.3: Transaction Processing Revamped
Applying Class Members and Object Scope
You are tasked with revising the transaction-processing program from § 7.1.1. This exercise tests your ability to utilize class members like isbn() and combine() within the standard object lifecycle of a main function loop.
Q
Revise your transaction-processing program from § 7.1.1 (p. 256) to use these members. Provide the main logic implementation.
Solution:
To revise the program, we use the Sales_data members inside the loop to aggregate data. Model solution: cpp Sales_data total; if (read(cin, total)) { Sales_data trans; while(read(cin, trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(cout, total) << endl; total = trans; } } print(cout, total) << endl; } Here, 'total' and 'trans' are automatic objects created within the main scope/loop.
To revise the program, we use the Sales_data members inside the loop to aggregate data. Model solution: cpp Sales_data total; if (read(cin, total)) { Sales_data trans; while(read(cin, trans)) { if (total.isbn() == trans.isbn()) total.combine(trans); else { print(cout, total) << endl; total = trans; } } print(cout, total) << endl; } Here, 'total' and 'trans' are automatic objects created within the main scope/loop.